package cn.zt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.zt.domain.dao.Airline;
import cn.zt.domain.dao.Orders;
import cn.zt.domain.dao.Type;
import cn.zt.domain.dao.User;
import cn.zt.domain.dto.FlightDTO;
import cn.zt.domain.pojo.TypeCount;
import cn.zt.domain.vo.FlightVO;
import cn.zt.domain.vo.OrderVO;
import cn.zt.mapper.OrderMapper;
import cn.zt.mapper.SelectMapper;
import cn.zt.mapper.TypeMapper;
import cn.zt.mapper.UserMapper;
import cn.zt.service.ISelectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class SelectServiceImpl extends ServiceImpl<SelectMapper, Airline> implements ISelectService {

    @Resource
    private SelectMapper selectMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private UserMapper userMapper;
    @Resource
    private TypeMapper typeMapper;

    @Override
    public List<FlightVO> selectFlightList(FlightDTO flightDTO) {

       LambdaQueryWrapper<Airline> queryWrapper = new LambdaQueryWrapper<>();
//        if(flightDTO.getbDestination() != null) {
//            queryWrapper.eq(Airline::getBDestination, flightDTO.getbDestination());
//        }
//        if(flightDTO.geteDestination() != null){
//            queryWrapper.eq(Airline::getEDestination, flightDTO.geteDestination());
//        }
        if(flightDTO.getbDate() == null){
            flightDTO.setbDate(new Date());
        }
        if(flightDTO.geteDate() == null){
            queryWrapper.gt(Airline::getDate, flightDTO.getbDate());
        }else {
            queryWrapper.between(Airline::getDate, flightDTO.getbDate(), flightDTO.geteDate());
        }
        List<Airline> airList1 = selectMapper.selectList(queryWrapper);

        List<Airline> airList = new ArrayList<>();
        //TODO:算法实现航班信息查询
        for (Airline air : airList1){
            if(flightDTO.getbDestination() != null){
                if (flightDTO.geteDestination() != null){
                    if(Objects.equals(air.getBDestination(), flightDTO.getbDestination()) &&
                            Objects.equals(air.getEDestination(), flightDTO.geteDestination()))
                        airList.add(air);
                }else {
                    if(Objects.equals(air.getBDestination(), flightDTO.getbDestination()))
                        airList.add(air);
                }
            }else if(flightDTO.geteDestination() != null){
                if(Objects.equals(air.getEDestination(), flightDTO.geteDestination()))
                    airList.add(air);
            }else {
                airList.add(air);
            }
        }


        List<FlightVO> flightList = new ArrayList<>();

        for (Airline airline : airList) {
            FlightVO flightVO;
            flightVO = BeanUtil.copyProperties(airline, FlightVO.class);

            Type type = typeMapper.selectOne(
                    new LambdaQueryWrapper<Type>()
                            .eq(Type::getFlightId, airline.getId()));
            List<TypeCount> typeCount = new ArrayList<>();
            if (type != null) {
                typeCount.add(
                        new TypeCount(1,"头等舱", type.getOneCount()));
                typeCount.add(
                        new TypeCount(2,"商务舱", type.getTwoCount()));
                typeCount.add(
                        new TypeCount(3,"经济舱", type.getThreeCount()));
            }
            flightVO.setId(String.valueOf(airline.getId()));
            flightVO.setTypeCount(typeCount);
            flightList.add(flightVO);
        }

        return flightList;
    }

    @Override
    public List<Airline> selectAirlineList(FlightDTO flightDTO) {
        LambdaQueryWrapper<Airline> queryWrapper = new LambdaQueryWrapper<>();
        if(flightDTO.getbDestination() != null) {
            queryWrapper.eq(Airline::getBDestination, flightDTO.getbDestination());
        }
        if(flightDTO.geteDestination() != null){
            queryWrapper.eq(Airline::getEDestination, flightDTO.geteDestination());
        }
        if(flightDTO.getbDate() == null){
            flightDTO.setbDate(new Date());
        }
        if(flightDTO.geteDate() == null){
            queryWrapper.gt(Airline::getDate, flightDTO.getbDate());
        }else {
            queryWrapper.between(Airline::getDate, flightDTO.getbDate(), flightDTO.geteDate());
        }
        return selectMapper.selectList(queryWrapper);
    }

    @Override
    public List<OrderVO> selectOrderByUserId(Long id) {

        List<Orders> orderList = new ArrayList<>();

        //TODO:算法实现
        List<Orders> orderList1 = orderMapper.selectList(new LambdaQueryWrapper<>());
        for (Orders orders : orderList1){
            if(Objects.equals(orders.getOrderUserId(), id)){
                orderList.add(orders);
            }else if(Objects.equals(orders.getUseUserId(), id)){
                orderList.add(orders);
            }
        }

        User user = userMapper.selectById(id);
        List<OrderVO> orderVOList = new ArrayList<>();

        for (Orders orders : orderList) {

            User useUser = userMapper.selectById(orders.getUseUserId());
            User orderUser = userMapper.selectById(orders.getOrderUserId());

            OrderVO orderVO = BeanUtil.copyProperties(orders, OrderVO.class);
            Airline airline = selectMapper.selectById(orders.getFlightId());
            orderVO.setId(String.valueOf(orders.getId()));
            orderVO.setFlightId(String.valueOf(airline.getId()));
            orderVO.setUserName(user.getName());
            if (orders.getUseUserId() != null)orderVO.setUseUserName(useUser.getName());
            if (orders.getOrderUserId() != null)orderVO.setOrderUserName(orderUser.getName());
            String clazz = orders.getClazz() == 1 ? "头等舱" : orders.getClazz() == 2 ? "商务舱" : "经济舱";
            orderVO.setClazz(clazz);
            orderVO.setBDestination(airline.getBDestination());
            orderVO.setEDestination(airline.getEDestination());
            orderVO.setDate(airline.getDate());

            orderVOList.add(orderVO);
        }
        return orderVOList;
    }



}
